#include "operation.h"

Operation::Operation()
{

}

Operation::Operation(QString sql){
//    target_column = new std::vector<QString>;
//    values = new std::vector<QString>;
    conditions = new std::vector<Condition>;
    this->sql = sql;//将sql赋值
    error_code = 0;
    //开始语法解析
    get_operation_type();

}

void Operation::get_operation_type(){
    QRegExp get_commit("\\s*(commit;)",Qt::CaseInsensitive);
    if(get_commit.indexIn(sql)!=-1){
       cout<<"commit;"<<endl;
       operation_type = "commit";
       return;
    }
    QRegExp get_rollback("\\s*(roll back;)",Qt::CaseInsensitive);
    if(get_rollback.indexIn(sql)!=-1){
       cout<<"roll back;"<<endl;
       operation_type = "rollBack";
       return;
    }

    QRegExp get_type("\\s*(\\w{1,})\\s");
    int pos = get_type.indexIn(sql);
    QString lower = get_type.cap(1).toLower();
    if(lower == "select"){
        operation_type = "select";
        handle_select();
    }else if(lower=="alter"){
        operation_type = "alter";
        handle_alter();
    }else if(lower=="update"){
        operation_type = "update";
        handle_update();
    }else if(lower=="insert"){
        operation_type = "insert";
        handle_insert();
    }else if(lower=="delete"){
        operation_type = "delete";
        handle_delete();
    }else if(lower=="create"){
        operation_type = "create";
        handle_create();
    }else if(lower=="drop"){
        operation_type = "drop";
        handle_drop();
    }
    else{
        error_code = INVALID_OPERATION_TYPE;
        std::cout<<"wrong operation type"<<std::endl;
    }
}

///aaaaa
bool Operation::add_modify_attribute(QString attributeString,int optype,QString tablename){//type表示是add还是modify
    //QString parameter = "name VARCHAR NOT NULL UNIQUE DEFAULT 'anonymous'";
    //下面这个用来解析一个属性对应属性名，约束
    QString pattern = "\\b(\\w+)\\b\\s+(\\w+)(?:\\s+(?!NOT NULL|UNIQUE|PRIMARY KEY|CHECK|DEFAULT)(\\w+))?(?:\\s+(NOT NULL|UNIQUE|PRIMARY KEY))?"
                     "(?:\\s+(?!NOT NULL|UNIQUE|PRIMARY KEY|CHECK|DEFAULT)(\\w+))?(?:\\s+(NOT NULL|UNIQUE|PRIMARY KEY))?"
                      "(?:\\s+(?!NOT NULL|UNIQUE|PRIMARY KEY|CHECK|DEFAULT)(\\w+))?(?:\\s+(NOT NULL|UNIQUE|PRIMARY KEY))?"
                      "(?:\\s+(?!NOT NULL|UNIQUE|PRIMARY KEY|DEFAULT|CHECK)(\\w+))?(?:\\s+DEFAULT\\s+(\\S+))?(?:\\s+CHECK\\s*\\((.*)\\))?";
                //(?:\\s+CHECK\\s+\\((.*)\\))?为什么就不行？？？？？？？？？？？
                //   "(?:\\s+CHECK\\s*\\((\\S+)\\))?";
    QRegExp attributeregex(pattern);


    if (attributeregex.indexIn(attributeString,Qt::CaseInsensitive) != -1) {
        QStringList capturedTexts = attributeregex.capturedTexts(); // 获取所有匹配结果

            QString name = capturedTexts[1]; // 属性名
            QString type = capturedTexts[2]; // 数据类型
            int typeno;
            QString notconstraints = capturedTexts[3]+capturedTexts[5]+capturedTexts[7]+capturedTexts[9]; // 其他错误约束条件，若改string非空，说明有错误约束，则给出错误信息
            QString defaultValue = capturedTexts[10]; // 默认值
            if(defaultValue==NULL) defaultValue="null";
            QString check=capturedTexts[11];
            QString checksymbol="null";
            QString checkvalue="null";


            bool isNotNull = capturedTexts.contains("NOT NULL"); // 是否有 "NOT NULL" 约束
            bool isUnique = capturedTexts.contains("UNIQUE"); // 是否有 "UNIQUE" 约束
            bool isPrimaryKey = capturedTexts.contains("PRIMARY KEY"); // 是否为主键

            //将数据类型转化成数字,并检查数据类型是否正确
            if(type.toLower()=="int"){
                typeno=1;
            }else if (type.toLower()=="float") {
                typeno=2;
            }else if(type.toLower()=="char"){
                typeno=3;
            }else if(type.toLower()=="varchar"){
                typeno=4;
            }else{
                std::cout<<"create error type!"<<std::endl;
                error_code = CREATE_ERROR_TYPE;
                 return false;
            }

            if(check!=NULL){


                //取出check左右两边的值以及他的符号
                QRegExp get_checkcontent("CHECK\\s*\\((\\w*)\\s*(>=|<|=|>|<=|!=)\\s*(\\w*)\\)");
                QRegExp get_checkcontentBetween("CHECK\\s*\\((\\w*)\\s*BETWEEN\\s*(\\w*)\\s*AND\\s*(\\w*)\\)");

                if(get_checkcontent.indexIn(attributeString,Qt::CaseInsensitive)!=-1){
                    QStringList checkTexts = get_checkcontent.capturedTexts();
                    QString check_content=get_checkcontent.cap(1);
                    //在这后面调用函数
                    checksymbol=get_checkcontent.cap(2);
                    checkvalue=get_checkcontent.cap(3);
                }else if(get_checkcontentBetween.indexIn(attributeString,Qt::CaseInsensitive)!=-1){
                    checksymbol="between";
                    checkvalue=get_checkcontentBetween.cap(2)+","+get_checkcontentBetween.cap(3);
                }

            }

            //出现错误约束
            if(notconstraints!=NULL){
                std::cout<<"check your constraint for "<<name.toStdString()<<std::endl;
                error_code = CREATE_CONSTRAINT_NOT_EXIST;
                 return false;
            }



            //判断主键和default值，若同时存在，给出错误信息
            if(isPrimaryKey&&defaultValue!=NULL){
                std::cout<<"Primary Key"<<name.toStdString()<<" cannot set default value!"<<std::endl;
                error_code = PRIMARY_KEY_AND_DEFAULT;
                return false;
            }

            //下面可以调用函数
            if(optype==3){

                if(isNotNull){
                    cout<<"ALTER ADD NOTNULL NO DEFAULT "<<endl;
                    error_code=Alter_ADD_NOT_NULL_ERROR;
                    if(defaultValue=="null") return false;
                }

                UserManager a = User1::instance()->globalManager;
                User1::instance()->globalManager.users[User1::instance()->userIndex].addAttribute(tablename.toStdString(),name.toStdString(),typeno,isPrimaryKey,isUnique,isNotNull,checksymbol.toStdString(),checkvalue.toStdString(),defaultValue.toStdString());

            }else if(optype==2){
                if(!User1::instance()->globalManager.users[User1::instance()->userIndex].modifyAttribute(tablename.toStdString(),name.toStdString(),typeno,isPrimaryKey,isUnique,isNotNull,checksymbol.toStdString(),checkvalue.toStdString(),defaultValue.toStdString())){
                   error_code=MODIFY_ERROR;
                }

            }else{
                User1::instance()->globalManager.users[User1::instance()->userIndex].addAttribute(tablename.toStdString(),name.toStdString(),typeno,isPrimaryKey,isUnique,isNotNull,checksymbol.toStdString(),checkvalue.toStdString(),defaultValue.toStdString());

            }

            // User1::instance()->globalManager.users[User1::instance()->userIndex].
                    //(tablename.toStdString(),get_checkcontent2.cap(1).toStdString(),get_checkcontent2.cap(2).toStdString(),get_checkcontent2.cap(3).toStdString())


    }else{
        error_code = ALTER_ERROR;
        std::cout<<"error input！"<<std::endl;
        return false;
    }
}


void Operation::handle_select(){
    QRegExp get_column("\\s?\\w{6}\\s+(\\S+)\\s+(\\w{4})\\s+(\\S{1,})");
    int pos = get_column.indexIn(sql);
    QString from = get_column.cap(2).toLower();
    if(from!="from"){
        error_code = SELECT_FROM_NOT_EXISTS;
        std::cout<<"from不存在"<<std::endl;
    }
    std::cout<<get_column.cap(1).toStdString()<<std::endl;
    std::cout<<get_column.cap(3).toStdString()<<std::endl;
    QString all_columns = get_column.cap(1);                                //cap(1)这里是所有的列
    QString all_tables = get_column.cap(3); //cap(3)这里是所有的表
    if(all_tables[all_tables.length()-1]==';'){
        all_tables.chop(1);
    }
    User1::instance()->currentTableIndex = findTableIndex(all_tables);
    target_table = all_tables;
    Condition* temp = nullptr;
    if(error_code==0){//说明前面都是正确的
        QRegExp check_where("(from|FROM)\\s+(\\S{1,})\\s+(\\w{5})");
        check_where.indexIn(sql);
        if(check_where.cap(3).toLower()!="where"){//说明sql语句中没有where
            conditions=nullptr;
            //下面检查没有where，表名的后面是否有其他符号,下面两句试试取出表名的字符串的长度并拼接到正则表达式中
            int length = all_tables.length();
            QString lenstr = QString::number(length);
            QRegExp check_chars("(from|FROM)\\s+(\\w{"+lenstr+"})\\s*;");
            check_chars.indexIn(sql);
//            QString test = check_chars.cap(1);
//            QString test_2 = check_chars_2.cap(1);
            if(check_chars.cap(2)!=all_tables){
                std::cout<<"where not exist"<<std::endl;
            }
        }else{//在这个语句块中将where后面的条件取出
            QRegExp get_conditions("(where|WHERE)\\s+(.{1,});");   //这里先不判断分号
            if(get_conditions.indexIn(sql)==-1){
                error_code = NO_SEMICOLON;
                std::cout<<"select没有分号"<<std::endl;
                return;
            }
            QString all_conditions = get_conditions.cap(2);//取出判断条件的字符串
            std::cout<<all_conditions.toStdString()<<std::endl;
            temp = parse_conditions(all_conditions.toStdString());//将判断条件的字符串进行解析
        }
    }

    //下面还要检查表是否存在，列是否存在
    int tableIndex = findTableIndex(target_table);
    if(tableIndex==-1){
        error_code = TABLE_NOT_FOUND;
        std::cout<<"table not found"<<std::endl;
        return;
    }

    parse_column(all_columns);//将得到的字符串进行解析,结果放入targetColumn中
    bool isSelectAll = false;
    for(auto each:target_column){
        if(findColumnIndex(target_table,each)==-1){
            if(each=="*"){
                isSelectAll = true;
                break;
            }
            cout<<each.toStdString()<<" not found"<<endl;
            error_code = COLUMN_NOT_FOUND;
            std::cout<<"column not found"<<std::endl;
            return;
        }
    }
    if(temp==nullptr){
        QVector<QString> tempVector1;
        if(!isSelectAll){
            for(auto each:target_column){
                tempVector1.push_back(each);
            }
        }else{
            for(auto each:User1::instance()->globalManager.users[User1::instance()->userIndex].tables[findTableIndex(all_tables)].attributes){
                tempVector1.push_back(QString::fromStdString(each.attriname));
            }
        }
        select_result.push_back(tempVector1);
        QVector<int> columnIndex;
        if(!isSelectAll){
            for(auto each:target_column){
                columnIndex.push_back(find_column_from_table(each,tableIndex));
            }
        }else{
            for(auto each:tempVector1){
                columnIndex.push_back(find_column_from_table(each,tableIndex));
            }
        }


        //下面是记录
        for(int i = 0;i<User1::instance()->globalManager.users[User1::instance()->userIndex].tables[tableIndex].data.rows.size();i++){
            QVector<QString> tempVector;
            for(int j = 0;j< columnIndex.size();j++){
                tempVector.push_back(QString::fromStdString(User1::instance()
                                     ->globalManager.users[User1::instance()->userIndex].tables[tableIndex].data.rows[i].columns[columnIndex[j]]));
            }
            select_result.push_back(tempVector);
        }
    }else{
        QVector<QString> tempVector1;
        if(!isSelectAll){
            for(auto each:target_column){
                tempVector1.push_back(each);
            }
        }else{
            for(auto each:User1::instance()->globalManager.users[User1::instance()->userIndex].tables[findTableIndex(all_tables)].attributes){
                tempVector1.push_back(QString::fromStdString(each.attriname));
            }
        }
        select_result.push_back(tempVector1);
        QVector<int> columnIndex;
        if(!isSelectAll){
            for(auto each:target_column){
                columnIndex.push_back(find_column_from_table(each,tableIndex));
            }
        }else{
            for(auto each:tempVector1){
                columnIndex.push_back(find_column_from_table(each,tableIndex));
            }
        }

        for(int i = 0;i<User1::instance()->globalManager.users[User1::instance()->userIndex].tables[tableIndex].data.rows.size();i++){
            vector<string> tempVector;
            for(int j = 0;j< columnIndex.size();j++){
                tempVector.push_back(User1::instance()
                                     ->globalManager.users[User1::instance()->userIndex].tables[tableIndex].data.rows[i].columns[columnIndex[j]]);
            }
            vector<vector<string>> toBeEvaluated;
            vector<string> tempVector2;
            for(auto each:tempVector1){
                tempVector2.push_back(each.toStdString());
            }
            toBeEvaluated.push_back(tempVector2);
            toBeEvaluated.push_back(tempVector);
            if(temp->evaluate(toBeEvaluated)){
                QVector<QString> qTempVector;
                for(auto each:tempVector){
                    qTempVector.push_back(QString::fromStdString(each));
                }
                select_result.push_back(qTempVector);
            }
        }
    }
}

void Operation::handle_insert(){

    QRegExp check_into("\\s*(\\w{6})\\s+(\\w{4})\\s+(\\w{1,})");
    int a = check_into.indexIn(sql);
    if(check_into.cap(2)!="into"){//insert 后面没有into
        error_code = INSERT_ERROR;
        std::cout<<"出错"<<std::endl;
        std::cout<<check_into.cap(2).toStdString()<<std::endl;
        return;
    }
    QString all_columns;
    QString all_tables = check_into.cap(3);//cap(3)这里是插入的的表

    std::cout<<all_tables.toStdString()<<std::endl;
    bool isInsertAll = false;//判断是否指定行插入还是全部插入
    QRegExp check_iIA("(\\)\\s*values\\s*\\()");
    if(check_iIA.indexIn(sql)==-1){
        isInsertAll = true;
        std::cout<<"insertAll"<<std::endl;
    }else{
        isInsertAll = false;
        std::cout<<"insertPart"<<std::endl;
        //insert into wck(sno, sname) values(1138, 'wck');

        //QRegExp get_columnName(all_tables+"\\s*\\((\\.{1,})\\)");
//        QRegExp get_columnName("(\\.*)?\\s*values");
//        get_columnName.indexIn(sql);
//        all_columns = get_columnName.cap(1);

        QRegExp tableIndex1(all_tables);
        QRegExp valuesIndex1("values",Qt::CaseInsensitive);
        int aa = tableIndex1.indexIn(sql)+all_tables.length();
        int b = valuesIndex1.indexIn(sql)-1;
        all_columns = sql.mid(aa,b-aa);
        all_columns = all_columns.trimmed();
        all_columns.remove(QChar('('),Qt::CaseSensitive);
        all_columns.remove(QChar(')'),Qt::CaseSensitive);
        std::cout<<all_columns.toStdString()<<std::endl;
    }
    //insert into stu values (1002, 'sssss');
    QRegExp get_insertValue("values\\s*(.{1,})\\s*;");
    QString values;
    if(get_insertValue.indexIn(sql)==-1){//说明没有分号
        error_code = NO_SEMICOLON;
        std::cout<<"没有分号"<<std::endl;
    }else{//有分号
        values = get_insertValue.cap(1).trimmed();
        if(values[0]!='(' or values[values.length()-1]!=')'){//说明括号不全
            std::cout<<"括号不全"<<std::endl;
            error_code = INCOMPLETE_PATENTHESES;
            return;
        }else{//得到所有的值（最后要按照空格分开）
            values.remove(QChar('('),Qt::CaseSensitive);
            values.remove(QChar(')'),Qt::CaseSensitive);
//            values.remove(QChar(' '),Qt::CaseSensitive);
            std::cout<<values.toLocal8Bit().toStdString()<<std::endl;
        }
    }    //后面要将各个结果的字符串按逗号分开，并处理
    int tableIndex = findTableIndex(all_tables);
    User1::instance()->currentTableIndex = tableIndex;
    if(tableIndex==-1){
        error_code = TABLE_NOT_FOUND;
        std::cout<<"table not found"<<std::endl;
        return;
    }
    if(!isInsertAll){
        QStringList columnList = all_columns.split(",");
        for(auto& each:columnList){
            each = each.trimmed();
            if(findColumnIndex(all_tables,each)==-1){
                cout<<each.toStdString()<<" not found"<<endl;
                error_code = COLUMN_NOT_FOUND;
                std::cout<<"column not found"<<std::endl;
                return;
            }
        }
        QStringList valueList = values.split(",");
        for(int i = 0;i<valueList.size();i++){
            valueList[i] = valueList[i].trimmed();
        }
        if(valueList.size()!=columnList.size()){
            error_code = COLUMNNUMBER_NOT_MATCH;
            std::cout<<"column size not match"<<std::endl;
            return;
        }
        //涉及到类型判断
        for(int i = 0;i<columnList.size();i++){
            if(User1::instance()->globalManager.users[User1::instance()->userIndex].
                    tables[User1::instance()->currentTableIndex].attributes[findColumnIndex(all_tables,columnList[i])].type !=is_int_float_varchar_char(valueList[i])){
                int bbbb = findColumnIndex(all_tables,columnList[i]);
                cout << "index: " << bbbb << endl;
                cout << "type: aaaaaaaaaaaaaaaaaaaaaaaaa" << User1::instance()->globalManager.users[User1::instance()->userIndex].
                        tables[User1::instance()->currentTableIndex].attributes[bbbb].type << endl;
                cout << "type: aaaaaaaaaaaaaaaaaaaaaaaaa" << is_int_float_varchar_char(valueList[i]) << endl;
                cout<<"columns did not match the values!"<<endl;
                error_code = COLUMNTYPE_NOT_MATCH;
                return;
            }
        }

        //换成vector
        vector<string> columns;
        for(auto column:columnList){
            columns.push_back(column.toStdString());
        }
        vector<string> Values;
        for(auto value:valueList){
            value = value.trimmed();
            value = value.remove('\'');
            Values.push_back(value.toStdString());
        }
        //insert into stu values(1098,'qbqbqb');
        bool isSucceeded = User1::instance()->globalManager.users[User1::instance()
                ->userIndex].addRecord(all_tables.toStdString(),columns,Values);
        if(!isSucceeded){
            cout<<"violate the constrains"<<endl;
            error_code = VIOLATE_CONSTRAINT;
            return;
        }else{
            //User1::instance()->globalManager.writeUser();
        }


    }else{
        //先判断个数是否正确
        QStringList valueList = values.split(",");
        if(valueList.size() != User1::instance()->globalManager.users[User1::instance()->userIndex].
                tables[User1::instance()->currentTableIndex].attributes.size()){
            error_code = COLUMNNUMBER_NOT_MATCH;
            std::cout<<"column not match"<<std::endl;
            return;
        }

        //再判断顺序
        for(int i = 0;i < valueList.size();i++){
            if(User1::instance()->globalManager.users[User1::instance()->userIndex].
                    tables[User1::instance()->currentTableIndex].attributes[i].type != is_int_float_varchar_char(valueList[i])){
                cout<<"wrong sequence or wrong type!"<<endl;
                error_code = WRONG_SEQUENCE;
                return;
            }
        }
        vector<string> row;
        Row temp;
        for(auto each:valueList){
            each.remove("'");

            row.push_back(each.toStdString());
        }
        temp.columns = row;
//        User1::instance()->globalManager.users[User1::instance()->userIndex].
//                            tables[User1::instance()->currentTableIndex].data.rows.push_back(temp);
//        User1::instance()->globalManager.writeTable(User1::instance()->getUserName().toStdString());
        bool isSucceeded = User1::instance()->globalManager.users[User1::instance()
                ->userIndex].addRecord(all_tables.toStdString(),row);
        if(!isSucceeded){
            cout<<"violate the constrains"<<endl;
            error_code = VIOLATE_CONSTRAINT;
            return;
        }else{
            //User1::instance()->globalManager.writeUser();
        }
    }
}

void Operation::handle_update(){
    QRegExp get_table("\\w{6}\\s+(\\S{1,})\\s+(set|SET)\\s+");
    get_table.indexIn(sql);
    QString tableName = get_table.cap(1);
    QString set = get_table.cap(2);
    QString condition;//update的条件
    QString modification;//修改的属性
    Condition* temp = nullptr;
    if(set!="set" and set!="SET"){//说明没有set
        error_code = UPDATE_ERROR;
        std::cout<<"update没有set"<<std::endl;
        return;
    }
    //下面检查是否存在where
    QRegExp check_where("\\s+(where||WHERE)\\s+");
    if(check_where.indexIn(sql)==-1){
        condition="";
        QRegExp get_modification("(set|SET)\\s+(.*);");
        if(get_modification.indexIn(sql)==-1){
            error_code = UPDATE_ERROR;
            std::cout<<"set后面条件错误"<<std::endl;
            return;
        }else{
            modification = get_modification.cap(2);
        }
    }else{//存在where，取出修改的属性和判断条件
        // update student set sno =123 where sname = wck;
        QRegExp get_ConAndMod("(set|SET)\\s+(.*)(where|WHERE)(.*);");
        if(get_ConAndMod.indexIn(sql)==-1){
            error_code = NO_SEMICOLON;
            std::cout<<"update没有分号"<<std::endl;
            return;
        }else{
            condition = get_ConAndMod.cap(4);
            modification = get_ConAndMod.cap(2);
            temp = parse_conditions(condition.toStdString());           //在这里对条件进行解析
        }
    }

    std::cout<<tableName.toStdString()<<std::endl;
    std::cout<<condition.toStdString()<<std::endl;
    std::cout<<modification.toStdString()<<std::endl;
    parse_modification(modification);
    if( (User1::instance()->currentTableIndex = findTableIndex(tableName)) ==-1){
        cout<<"table not found"<<endl;
        error_code = TABLE_NOT_FOUND;
        return;
    }
    //下面判断更新的列是否存在
    for(auto temp:modificationMap){
        if(findColumnIndex(tableName,temp.first)==-1){
            cout<<"column not found"<<endl;
            error_code = COLUMN_NOT_FOUND;
            return;
        }
        if(User1::instance()->globalManager.users[User1::instance()->userIndex].
                tables[User1::instance()->currentTableIndex].attributes[findColumnIndex(tableName,temp.first)].type !=is_int_float_varchar_char(temp.second)){
            cout<<"columns did not match the values!"<<endl;
            error_code = COLUMNTYPE_NOT_MATCH;
            return;
        }
    }
    if(temp){
        vector<string>head;
        for(int i = 0;i<User1::instance()->globalManager.users[User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].attributes.size();i++){
            head.push_back(User1::instance()->globalManager.users[User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].attributes[i].attriname);
        }
        for(int i = 0;i<User1::instance()->globalManager.users[User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].data.rows.size();i++){
            vector<string> tempVector;
            for(int j = 0;j< User1::instance()->globalManager.users[User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].data.rows[i].columns.size();j++){
                tempVector.push_back(User1::instance()
                                     ->globalManager.users[User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].data.rows[i].columns[j]);
            }
            vector<vector<string>> toBeEvaluated;
            toBeEvaluated.push_back(head);
            toBeEvaluated.push_back(tempVector);
            bool isSuccesseed = temp->evaluate(toBeEvaluated);
            if(isSuccesseed){
                for(auto each:modificationMap){
                    each.second = each.second.remove('\'');
                    each.second = each.second.remove('\"');
                    if(User1::instance()->globalManager.users[User1::instance()->userIndex].updateData
                            (tableName.toStdString(),each.first.toStdString(),i+1,each.second.toStdString())){
                        ;
                    }else {
                        cout<<"violate the constraints"<<endl;
                        error_code = VIOLATE_CONSTRAINT;
                        return;
                    }
                }
            }
        }
        //User1::instance()->globalManager.writeUser();
    }else{
        for(int i = 0;i<User1::instance()->globalManager.users[User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].data.rows.size();i++){
            vector<string> tempVector;
            for(int j = 0;j< User1::instance()->globalManager.users[User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].data.rows[i].columns.size();j++){
                tempVector.push_back(User1::instance()
                                     ->globalManager.users[User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].data.rows[i].columns[j]);
            }
            for(auto each:modificationMap){
                each.second = each.second.remove('\'');
                each.second = each.second.remove('\"');
                if(User1::instance()->globalManager.users[User1::instance()->userIndex].updateData
                        (tableName.toStdString(),each.first.toStdString(),i+1,each.second.toStdString())){
                    ;
                }else {
                    cout<<"violate the constraints"<<endl;
                    error_code = VIOLATE_CONSTRAINT;
                    return;
                }
            }

        }
        //User1::instance()->globalManager.writeUser();
    }


}

void Operation::handle_delete(){
    QRegExp check_from("\\s*(delete|DELETE)\\s+(\\w{4})\\s+");
    check_from.indexIn(sql);
    QString tableName;
    QString conditions;
    Condition* temp;
    if(check_from.cap(2).toLower()!="from"){
        error_code = DELETE_ERROR;
        std::cout<<"from not found"<<std::endl;
        return;
    }
    QRegExp check_semicolon(";$");
    if(check_semicolon.indexIn(sql)==-1){
        error_code = NO_SEMICOLON;
        std::cout<<"semicolon not found"<<std::endl;
        return;
    }
    QRegExp get_table("(from|FROM)\\s+(\\S*)\\s+");
    if(get_table.indexIn(sql)==-1){
        error_code = DELETE_ERROR;
        std::cout<<"table name not found"<<std::endl;
        return;
    }else {
        tableName = get_table.cap(2);
    }
    QRegExp check_where("\\s+(where||WHERE)\\s+");
    if(check_where.indexIn(sql)==-1){
        conditions="";
    }else{
        QRegExp get_condition("(where|WHERE)\\s+(.*);");
        if(get_condition.indexIn(sql)==-1){
            std::cout<<"condition not found"<<std::endl;
            error_code = DELETE_ERROR;
            return;
        }else {
            conditions = get_condition.cap(2);
            temp = parse_conditions(conditions.toStdString());              //在这里对条件进行解析
        }
    }
    std::cout<<conditions.toStdString()<<std::endl;
    std::cout<<tableName.toStdString()<<std::endl;
    if(findTableIndex(tableName)==-1){
            error_code = TABLE_NOT_FOUND;
            cout<<"table not found"<<endl;
            return;
        }
        if(temp == nullptr && error_code==0){
            User1::instance()->
                    globalManager.users[User1::instance()->userIndex].tables[findTableIndex(tableName)].data.rows.clear();
            //User1::instance()->
                //    globalManager.writeData(User1::instance()->username.toStdString(),tableName.toStdString());
        }else{
            if(error_code!=0){
                return;
            }
            vector<string> head;

            for(int i = 0;i<User1::instance()->
                globalManager.users[User1::instance()->userIndex].tables[findTableIndex(tableName)].attributes.size();i++){
                head.push_back(User1::instance()->globalManager.users[User1::instance()->userIndex].tables[findTableIndex(tableName)].attributes[i].attriname);
            }
            for(int i = 0;i<User1::instance()->
                globalManager.users[User1::instance()->userIndex].tables[findTableIndex(tableName)].data.rows.size();i++){
                vector<vector<string>> toBeEvaluate;
                vector<string> data;
                toBeEvaluate.push_back(head);
                for(int j = 0;j<User1::instance()->
                    globalManager.users[User1::instance()->userIndex].tables[findTableIndex(tableName)].data.rows[i].columns.size();j++){
                    data.push_back(User1::instance()->
                                   globalManager.users[User1::instance()->userIndex].tables[findTableIndex(tableName)].data.rows[i].columns[j]);
                }
                toBeEvaluate.push_back(data);
                bool del = temp->evaluate(toBeEvaluate);
                if(del){
                    User1::instance()->
                    globalManager.users[User1::instance()->userIndex].
                    tables[findTableIndex(tableName)].data.rows.erase(User1::instance()->
                                                                      globalManager.users[User1::instance()->userIndex].
                                                                      tables[findTableIndex(tableName)].data.rows.begin()+i);
                    i--;
                }
            }
//            User1::instance()->
//                    globalManager.writeTable(User1::instance()->username.toStdString());
        }

}


void Operation::parse_column(QString columns){
    QRegExp check_comma(",\\s*,");
    if(check_comma.indexIn(columns)!=-1){
        error_code = COLUMN_ERROR;
        std::cout<<"wrong input column"<<std::endl;
        return;
    }
    QStringList list = columns.split(",");
    for(QString each:list){
        target_column.push_back(each);
        std::cout<<each.toStdString()<<std::endl;
    }
}


void Operation::handle_drop(){
    QRegExp check_table("(drop|DROP)\\s+(table|TABLE)\\s+");
    if(check_table.indexIn(sql)==-1){
        error_code = DROP_ERROR;
        std::cout<<"missing word 'table'"<<std::endl;
        return;
    }
    QRegExp get_table("(table|TABLE)\\s+(\\S+)\\s*;");
    if(get_table.indexIn(sql)==-1){
        error_code = NO_SEMICOLON;
        std::cout<<"semicolon not found"<<std::endl;
        return;
    }else{
        target_table = get_table.cap(2);
        std::cout<<target_table.toStdString()<<std::endl;
    }


}

void Operation::parse_modification(QString modification){
    QRegExp check_comma(",\\s*,");
    if(check_comma.indexIn(modification)!=-1){
        error_code = COLUMN_ERROR;
        std::cout<<"wrong input column"<<std::endl;
        return;
    }
    QRegExp get_modification("(\\w+)\\s*((=)\\s*(\\S+))");
//    if(get_modification.indexIn(modification)){
//        error_code = MODIFICATION_ERROR;
//        std::cout<<"invalid modification"<<std::endl;
//        return;
//    }
//    int pos = 0;
//    while ((pos = get_modification.indexIn(modification,pos))!=-1) {
//        modificationMap[get_modification.cap(1)] = get_modification.cap(4);
//        pos+=get_modification.matchedLength();
//    }
    QStringList aList = modification.split("and");
    for(auto each:aList){
        if(get_modification.indexIn(each)==-1){
            error_code = MODIFICATION_ERROR;
            std::cout<<"invalid modification"<<std::endl;
            return;
        }else{
            modificationMap[get_modification.cap(1)] = get_modification.cap(4);
        }
    }
    //下面用来输出以进行调试
    for(auto temp:modificationMap){
        QString column = temp.first;
        QString values = temp.second;
        std::cout<<column.toStdString()<<" = "<<values.toStdString()<<std::endl;
    }
}

Condition* Operation::parse_conditions(std::string where){
    //select sno,sname from wck3 where sno>1138 and sname like '钱*';
    std::vector<std::string>conditions;
    bool flag = false;
       int nest_level = 0;
       int start = 0;
       for(int i = 0;i<where.length();i++){
           if(where[i]=='('){
               nest_level++;
           }else if(where[i]==')'){
               nest_level--;
               if(nest_level==0){
                   conditions.push_back(where.substr(start,i-start+1));
                   start = i+1;
               }
           }else if(where[i]==' ' && nest_level==0 && i>start &&(where[i+1]=='a'||where[i+1]=='o'||where[i+1]=='O'||where[i+1]=='A'||where[i+1]=='(')) {
               conditions.push_back(where.substr(start,i-start));
               start = i+1;
           }
       }
       if(start<where.length()){//最后一个条件
           conditions.push_back(where.substr(start));
       }

    std::vector<std::string> ops = {"AND","OR","and","or"};
    Condition* root = new Condition();
    Condition* current = root;
    for(auto& temp:conditions){
        temp = QString::fromStdString(temp).trimmed().toStdString();
        if(temp[0]=='(' && temp[temp.length()-1] ==')'){
            current->operands.push_back(parse_conditions(temp.substr(1,temp.length()-2)));
        }else if(std::find(ops.begin(),ops.end(),temp)!=ops.end()){
            current->op = temp;
        }else if(current->field.empty()){
            int operator_index = temp.find_first_of("<>=!");
            if(operator_index==-1){
                //运算符可能是Like
                QRegExp check_Like("\\s*(\\w+)\\s+like\\s*(\\S+)");
                //select sno from stu where name like "*张";
                if(check_Like.indexIn(QString::fromStdString(temp),Qt::CaseInsensitive) != -1){
                    current->op = "like";
                    current->field = check_Like.cap(1).toStdString();
                    QString tempValue = check_Like.cap(2).remove(0,1);
                    tempValue.chop(1);
                    current->value = tempValue.toStdString();
                    //检查列是否存在
                    if(findColumnIndex(QString::fromStdString(User1::instance()->globalManager.users
                                                              [User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].tablename),check_Like.cap(1))==-1){
                        cout<<"this table dont have such column"<<endl;
                        error_code = COLUMN_NOT_FOUND;
                        return nullptr;
                    }
                }

            }else{
                int count = 1;
                if(temp[operator_index]=='='&&temp[operator_index+1]=='='){
                    cout<<"invalid operator"<<endl;
                    error_code = INVALID_OPERATOR;
                    return nullptr;
                }
                if(temp[operator_index+1]==' '){
                    ;
                }else if(temp[operator_index+1]=='='){
                    count = 2;
                }
                current->field = temp.substr(0, operator_index);
                current->op = temp.substr(operator_index, count);
                current->value = temp.substr(operator_index + count);
                QString tempField = QString::fromStdString(current->field);
                QString temp1 = tempField.trimmed();
                current->field = temp1.toStdString();
                QString tempValue = QString::fromStdString(current->value);
                tempValue = tempValue.trimmed();
                if(tempValue[0]=='"'||tempValue[0]=='\''){
                    tempValue.remove(0,1);
                    tempValue.chop(1);
                }
                current->value = tempValue.toStdString();
                //检查列是否存在
                if(findColumnIndex(QString::fromStdString(User1::instance()->globalManager.users
                          [User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].tablename),QString::fromStdString(current->field))==-1){
                    cout<<"this table dont have such column"<<endl;
                    error_code = COLUMN_NOT_FOUND;
                    return nullptr;
                }
            }
        }else{
            Condition* new_node = new Condition();
            new_node->field = current->field;
            new_node->op = current->op;
            new_node->value = current->value;
            current->field = "";
            current->op = temp;
            current->operands.push_back(new_node);
        }
    }
    return root;
}

int Operation::find_column_from_table(QString columnName,int tableIndex){
    for(int i = 0;i<User1::instance()->globalManager.users[User1::instance()->userIndex].tables[tableIndex].attributes
        .size();i++){
        if(columnName.toStdString()==User1::instance()->globalManager.users[User1::instance()->userIndex].tables[tableIndex].attributes[i].attriname){
            return i;
        }
    }
    return -1;
}

int Operation::findTableIndex(QString tableName){
    int tableIndex = 0;
    bool tempBool = false;
    for(auto table:User1::instance()->globalManager.users[User1::instance()->userIndex].tables){
        if(table.tablename==tableName.toStdString()){
            tempBool = true;
            break;
        }
        tableIndex++;
    }
    if(!tempBool){
        return -1;
    }else{
        return tableIndex;
    }
}
int Operation::findColumnIndex(QString tableName1,QString columnName){
    bool boolTemp = false;
    int columnIndex = 0;
    UserManager abcd = User1::instance()->globalManager;
    //cout<<"currentIndex:"<<findTableIndex(tableName1)<<endl;
    for(int i = 0;i<User1::instance()->globalManager.users[User1::instance()->userIndex].tables[findTableIndex(tableName1)].attributes.size();i++){
//        cout<<"debug debug debug:"<<User1::instance()->globalManager.users[User1::instance()->userIndex].tables[findTableIndex(tableName1)].attributes[i].attriname<<endl;
        if(User1::instance()->globalManager.users[User1::instance()->userIndex].tables[findTableIndex(tableName1)].attributes[i].attriname==columnName.toStdString()){
            boolTemp = true;
            break;
        }
        columnIndex++;
    }
//    for(auto column:User1::instance()->globalManager.users[0].tables[findTableIndex(tableName1)].attributes){
//        cout<<"debug debug debug:"<<column.attriname<<endl;
//        if(column.attriname==columnName.toStdString()){
//            boolTemp = true;
//            break;
//        }
//        columnIndex++;
//    }
    if(!boolTemp){
        return -1;
    }else{
        return columnIndex;
    }
}

int Operation::is_int_float_varchar_char(QString value){
    bool isInt;
    bool isFloat;
    bool isVarchar;
    bool isChar;

    value.toInt(&isInt);
    if (isInt){
        //std::cout<<"int"<<std::endl;
        return 1;}
    value.toDouble(&isFloat);
    if(isFloat){
        std::cout<<"float"<<std::endl;
         return 2;
    }
    QRegExp is_char_varchar("'(.*)'");
    if(is_char_varchar.indexIn(value)==-1){

        std::cout<<"error"<<std::endl;
        return -1;
    }else{
        if(is_char_varchar.cap(1).length()==1){
            isChar=true;

            std::cout<<"char"<<std::endl;
            return 3;
        }else{
            isVarchar=true;

            std::cout<<"varchar"<<std::endl;
            return 4;
        }
    }

}


















void Operation::execute(){
    //在这里判断各种条件是否正确，表与列是否存在
    if(error_code!=0){
        std::cout<<"wrong input"<<std::endl;
        return;
    }
    if(operation_type=="create"){//TODO

    }
}





//aaaaaaaaaaaa
void Operation::handle_create(){
    QRegExp check_table("(create|CREATE)\\s+(table|TABLE|database|DATABASE)");
    bool isTable;
    int attributesnum;
    QString tablename;//表名
    QString attributes;

    //判断是table还是database
    if(check_table.indexIn(sql)==-1,Qt::CaseInsensitive){
        std::cout<<"create 后面没有 table或者database"<<std::endl;
        error_code = CREATE_ERROR;
        return;
    }else{
        if(check_table.cap(2).toLower()=="table"){
            isTable = true;
        }else{
            isTable = false;
        }
    }
    if(isTable){
        QRegExp get_Name("(table|TABLE)\\s+(\\S*)\\s*\\(");

        if(get_Name.indexIn(sql,Qt::CaseInsensitive)==-1){
            std::cout<<"table name not found"<<std::endl;
            error_code = CREATE_ERROR;
            return;
        }

        tablename = get_Name.cap(2);//取出表名
        if(!User1::instance()->globalManager.users[User1::instance()->userIndex].addTable(tablename.toStdString())){
            error_code = CREATE_ERROR;
        }
//        User1::instance()->globalManager.writeTable(User1::instance()->username.toStdString());
        //下面取出各种属性，column1 data_type,column2 data_type, unique（column1，column2）
        QRegExp get_Attributes(tablename+"\\s*\\((.*)\\);");
        if(get_Attributes.indexIn(sql)==-1){
            error_code = NO_SEMICOLON;
            std::cout<<"semicolon not found"<<std::endl;
            return;
        }

        attributes = get_Attributes.cap(1);
        std::cout<<attributes.toStdString()<<std::endl;

        QStringList parameterList = attributes.split(",", QString::SkipEmptyParts);  // 拆分为逗号分隔的参数列表
        //QString pattern = "\\b(\\w+\\s+\\w+|\\w+\\([^)]+\\))\\b";
//        QRegExp regex(pattern);
//        QStringList parameterList;
//        int pos = 0;
//            while ((pos = regex.indexIn(attributes, pos)) != -1) {
//                QString capturedText = regex.cap(1).trimmed();
//                parameterList.append(capturedText);
//                qDebug() << capturedText;

//                pos += regex.matchedLength();
//            }
            for (QString parameter : parameterList) {
                parameter = parameter.trimmed();  // 去掉空格

                //可以在这先取出前六个字符，判断是不是check（表一级的）
                if(parameter.left(5).toLower()=="check"){
                    QRegExp get_checkcontent("CHECK\\s*\\((.*)\\)");
                    if(get_checkcontent.indexIn(parameter,Qt::CaseInsensitive)==-1){
                        error_code = CREATE_ERROR;
                       std::cout<<"check content cannot be null"<<std::endl;
                       User1::instance()->globalManager.users[User1::instance()->userIndex].deleteTable(tablename.toStdString());
                        return;
                    }


                    //取出check左右两边的值以及他的符号
                    QRegExp get_checkcontent2("CHECK\\s*\\((\\w*)\\s*(>=|<|=|>|<=|!=)\\s*(\\w*)\\)");
                    QRegExp get_checkcontentBetween("CHECK\\s*\\((\\w*)\\s*BETWEEN\\s*(\\w*)\\s*AND\\s*(\\w*)\\)");
                    if(get_checkcontent2.indexIn(parameter)!=-1){

                        // QStringList capturedTexts = get_checkcontent2.capturedTexts();
                         QString check_content=get_checkcontent.cap(1);
                          std::cout<<get_checkcontent2.cap(1).toStdString()<<std::endl;
                          std::cout<<get_checkcontent2.cap(2).toStdString()<<std::endl;
                          std::cout<<get_checkcontent2.cap(3).toStdString()<<std::endl;
                         //在这后面调用函数
                       User1::instance()->globalManager.users[User1::instance()->userIndex].modifyCheck(tablename.toStdString(),get_checkcontent2.cap(1).toStdString(),get_checkcontent2.cap(2).toStdString(),get_checkcontent2.cap(3).toStdString());

                    }else if(get_checkcontentBetween.indexIn(parameter)!=-1){
                        QString betweenvalue=get_checkcontentBetween.cap(2)+","+get_checkcontentBetween.cap(3);
                        User1::instance()->globalManager.users[User1::instance()->userIndex].modifyCheck(tablename.toStdString(),get_checkcontentBetween.cap(1).toStdString(),"between",betweenvalue.toStdString());

                    }




                }else if(parameter.left(11).toLower()=="primary key"){
                    QRegExp get_primarykey("PRIMARY KEY\\s*\\((.*)\\)",Qt::CaseInsensitive);

                    if(get_primarykey.indexIn(parameter)==-1){
                        error_code = CREATE_ERROR;
                        std::cout<<"primary key content cannot be null"<<std::endl;
                        User1::instance()->globalManager.users[User1::instance()->userIndex].deleteTable(tablename.toStdString());

                        return;
                    }

                    QStringList primaryList = get_primarykey.cap(1).split(" ", QString::SkipEmptyParts);  // 拆分为逗号分隔的参数列表


                    std::vector<std::string> vectorString;
                    vectorString.reserve(primaryList.size());  // 为了提高性能，提前分配空间

                    for (const QString& str : primaryList) {
                        vectorString.push_back(str.toStdString());
                    }


                  //  QString primarykey_content=get_primarykey.cap(1);
                    //可在此调用函数
                     User1::instance()->globalManager.users[User1::instance()->userIndex].modifyPrimaryKey(tablename.toStdString(),vectorString);

                }else if(parameter.left(6).toLower()=="unique"){
                    QRegExp get_unique("UNIQUE\\s*\\((.*)\\)",Qt::CaseInsensitive);
                    if(get_unique.indexIn(parameter,Qt::CaseInsensitive)==-1){
                        error_code = CREATE_ERROR;
                        std::cout<<"unique content cannot be null"<<std::endl;
                        User1::instance()->globalManager.users[User1::instance()->userIndex].deleteTable(tablename.toStdString());

                        return;
                    }

                    QStringList primaryList = get_unique.cap(1).split(" ", QString::SkipEmptyParts);  // 拆分为逗号分隔的参数列表
                   // std::vector<std::string> vectorString(primaryList.begin(),primaryList.end());
                    //下面开始将list转换成vector
                    std::vector<std::string> vectorString;
                    vectorString.reserve(primaryList.size());  // 为了提高性能，提前分配空间

                    for (const QString& str : primaryList) {
                        vectorString.push_back(str.toStdString());
                    }

                    User1::instance()->globalManager.users[User1::instance()->userIndex].modifyUnique(tablename.toStdString(),vectorString);


                    //QString unique_content=get_unique.cap(1);
                }else
                {
                  if(!add_modify_attribute(parameter,1,tablename)){//1代表add
                      std::cout<<"增加属性失败！"<<std::endl;
                      error_code = CREATE_PARAMENT_LIST_ERROR;
                      User1::instance()->globalManager.users[User1::instance()->userIndex].deleteTable(tablename.toStdString());
                      return;
                  }
                }
       }

    }else{//如果创建的是数据库
        QRegExp get_Name("(database|DATABASE)\\s+(\\S*)\\s*;");
        if(get_Name.indexIn(sql)==-1){
            std::cout<<"database name not found or no semicolon"<<std::endl;
            error_code = CREATE_ERROR;
            return;
        }
        tablename = get_Name.cap(2);//取出数据库名

        //create_database(String databasesname);
      //  User1::instance()->globalManager.users[User1::instance()->userIndex]????

    }
    User1::instance()->globalManager.writeUser();
    std::cout<<tablename.toStdString()<<std::endl;
}

void Operation::handle_alter(){
    UserManager b = User1::instance()->globalManager;
    QRegExp check_alterTable("\\s*(alter|ALTER)\\s+(table|TABLE)");
    QString alter_operation;//modify、drop、add后面具体的值
    if(check_alterTable.indexIn(sql)==-1){
        error_code=ALTER_ERROR;
        std::cout<<"missing word 'table'"<<std::endl;
        return;
    }
    QRegExp get_nameAndType("(table|TABLE)\\s+(\\S*)\\s+(modify|MODIFY|ADD|add|drop|DROP)");
    if(get_nameAndType.indexIn(sql)==-1){
        error_code = ALTER_ERROR;
        std::cout<<"table name not found or invalid alter operation"<<std::endl;
        return;
    }else{
        target_table = get_nameAndType.cap(2);
        alter_type = get_nameAndType.cap(3).toLower();
        std::cout<<target_table.toStdString()<<std::endl;
        std::cout<<alter_type.toStdString()<<std::endl;
    }
     QString alter_type=get_nameAndType.cap(3);//这个用来记录到底是modify add 还是 delete

    //下面开始分类，具体是alter的哪一类
    if(alter_type.toLower()=="modify"){//alter table student modify sname varchar(8) unique   Oracle支持没有column的语法，所以这里有无column都可被解析
                                      //ALTER TABLE table_name MODIFY COLUMN column_name data_type;

       // QRegExp get_modify_message(alter_type+"\\s*(column|COLUMN)?\\s+(\\w*)+\\s+(\\w*)\\s*(.*)\\s*;",Qt::CaseInsensitive);
        QRegExp get_modify_message(alter_type+"\\s*(column|COLUMN)?\\s+(.*)\\s*;",Qt::CaseInsensitive);
        if(get_modify_message.indexIn(sql)==-1){
            error_code=ALTER_ERROR;
            std::cout<<"check your  "<<std::endl;
            return;
        }

        if(!add_modify_attribute(get_modify_message.cap(2),2, target_table)){
             std::cout<<"modify error!Check your content! "<<std::endl;

             return;
        }


        QString modifyColumnName= get_modify_message.cap(2);//modify列的名字
        QString modifyColumnType= get_modify_message.cap(3);//modify列的类型
        QString modifyColumnCondition= get_modify_message.cap(4);//modify列的完整性约束

        std::cout<<"modify column name:"<<modifyColumnName.toStdString()<<std::endl;
        std::cout<<"modify column type:"<<modifyColumnType.toStdString()<<std::endl;
        std::cout<<"modify column condition:"<<modifyColumnCondition.toStdString()<<std::endl;

//        QString test="'a'";
//        QString result=is_int_float_varchar_char(test);

//        std::cout<<result.toStdString()<<std::endl;


    }else if(alter_type.toLower()=="add"){//alter table student add register_date timestamp default now();
        //ALTER TABLE table_name ADD COLUMN column_name data_type;
       // QRegExp get_add_message(alter_type+"\\s*(column|COLUMN)?\\s+(\\w+)+\\s+(\\w*)+\\s+(.*)\\s*;", Qt::CaseInsensitive);
        QRegExp get_add_message(alter_type+"\\s*(column|COLUMN)?\\s+(.*)\\s*;", Qt::CaseInsensitive);

        if(get_add_message.indexIn(sql)==-1){
            error_code=ALTER_ERROR;
            std::cout<<"check your  "<<std::endl;
            return;
        }

        if(!add_modify_attribute(get_add_message.cap(2),3, target_table)){
            std::cout<<"add column error!Check your content!"<<std::endl;
            //error_code
            return;

        }
        UserManager a = User1::instance()->globalManager;
        QString addColumnName= get_add_message.cap(2);//增加列的名字
        QString addColumnType= get_add_message.cap(3);//增加列的类型
        QString addColumnCondition= get_add_message.cap(4);//增加列的完整性约束



        std::cout<<"add column name:"<<addColumnName.toStdString()<<std::endl;
        std::cout<<"add column type:"<<addColumnType.toStdString()<<std::endl;
        std::cout<<"add column condition"<<addColumnCondition.toStdString()<<std::endl;



    }else {//alter table student drop register_date;
           //alter table student drop column register_date;
         QRegExp get_drop_message(alter_type+"\\s*(column|COLUMN)?\\s+(\\w+)\\s*;",Qt::CaseInsensitive);

         if(get_drop_message.indexIn(sql)==-1){
             error_code=ALTER_ERROR;
             std::cout<<"check your drop syntax!"<<std::endl;
             return;
         }
//         UserManager a;
//         a.readUser();
      //  parse_column();
         QString dropColumnName= get_drop_message.cap(2);//drop列的名字
         std::cout<<"drop column name:"<<dropColumnName.toStdString()<<std::endl;
         User1::instance()->globalManager.users[User1::instance()->userIndex].dropAttribute(target_table.toStdString(),dropColumnName.toStdString());
//         UserManager b;
//         b.readUser();
         cout << "aaa";
    }
    UserManager a = User1::instance()->globalManager;
    User1::instance()->globalManager.writeUser();
    return;
}
